package com.lkd.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.constant.StatusConstant;
import com.lkd.dto.PartnerPageDto;
import com.lkd.entity.Partner;
import com.lkd.exception.BusinessException;
import com.lkd.mapper.PartnerMapper;
import com.lkd.result.PageBean;
import com.lkd.service.PartnerService;
import com.lkd.vo.PartnerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PartnerServiceImpl implements PartnerService {
    //自动注入PartnerMapper接口
    @Autowired
    private PartnerMapper partnerMapper;


    @Override
    //新增合作商
    public Boolean save(Partner partner) {
        partner.setStatus(StatusConstant.ENABLE);
        Boolean result = partnerMapper.save(partner);
        return result;
    }

    @Override
    //分页查询合作商
    public PageBean getPage(PartnerPageDto partnerPageDto) {
        PageHelper.startPage(partnerPageDto.getPageIndex(),partnerPageDto.getPageSize());
        List<PartnerVo> list = partnerMapper.getPage(partnerPageDto);
        list.forEach(partnerVo ->{
           Integer vmCount = partnerMapper.getVmCount(partnerVo.getId());
           partnerVo.setVmCount(vmCount);
        });
        Page<PartnerVo> page = (Page<PartnerVo>) list;
        return new PageBean(page.getPageNum(),page.getPageSize(), (long) page.getPages(),page.getTotal(),page.getResult());
    }


    @Override
    //根据ID查询合作商
    public Partner getById(Integer id) {
        return partnerMapper.getById(id);
    }

    @Override
    //根据ID修改合作商
    public Boolean update(Partner partner) {
        return partnerMapper.update(partner);
    }

    @Override
    //根据ID删除合作商
    public Boolean deleteById(Integer id) {
        return partnerMapper.deleteById(id);
    }

    @Override
    //查询合作商总数
    public Integer count() {
        Integer count = partnerMapper.count();
        if(count ==null || count == 0){return 0;}
        return count;
    }
}
